package com.xunkids.bos.migration.business.service.bos.impl;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson2.JSON;
import com.xunkids.bos.migration.common.constant.BusinessConstant;
import com.xunkids.bos.migration.component.transmission.pojo.RequestMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Service;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @program: bos-migration
 * @author: byl
 * @create: 2024-03-29 16:36
 * @Description
 */

@Service
public class BosEventServiceBbWatchImpl implements InitializingBean, DisposableBean {

    private final Logger logger = LoggerFactory.getLogger(BosEventServiceBbWatchImpl.class);
    private final AtomicInteger state = new AtomicInteger(-1);




    @Override
    public void destroy() {
        state.set(-1);

    }

    @Override
    public void afterPropertiesSet() {
        this.state.set(0);

    }


    public void doRun(RequestMessage message) {
        message.data().ifPresent(msg -> {
            logger.error("百度入参" + JSON.toJSONString(msg));
            String post = HttpUtil.post(BusinessConstant.XIAOMI_CONSUMER_URL, JSON.toJSONString(msg));
        });

//        Optional<BosEventDto> data = message.data();
//        if (data.isPresent()) {
//            BosEventDto bosEventDto = data.get();
//            BosContent content = bosEventDto.getContent();
//            //判断设备类型
//            if (chainService.queryPathAndDeviceType(content.getBucket(), content.getObject())) {
//                if (bosEventDto.getEventType().equalsIgnoreCase(BusinessConstant.PUT_OBJECT)) {
//                    //获取bos object
//                    BOSStorage instance = BOSStorage.getInstance();
//                    BosClient bosClient = instance.getBosClient();
//                    OSStorage osStorage = OSStorage.getInstance();
//                    try {
//                        OSS ossClient = osStorage.getOssClient();
//                        boolean b = bosClient.doesObjectExist(content.getBucket(), content.getObject());
//                        if (b ) {
//                            BosObject object = bosClient.getObject(content.getBucket(), content.getObject());
//                            if (ObjectUtil.isNotEmpty(object)) {
//                                ObjectMetadata ossMetadata = new ObjectMetadata();
//                                com.baidubce.services.bos.model.ObjectMetadata bosMetadata = object.getObjectMetadata();
//                                if(ObjectUtil.isNotEmpty(bosMetadata)){
//                                    bosMetadata.getUserMetadata().forEach(ossMetadata::addUserMetadata);
//                                }
//                                BosObjectInputStream objectContent = object.getObjectContent();
//                                try  {
//                                    //存入阿里 OSS
//                                    ossClient.putObject(
//                                            BusinessConstant.XIAOMI_BUCKET_PREFIX + content.getBucket() + BusinessConstant.XIAOMI_BUCKET_SUFFIX
//                                            , content.getObject(), objectContent,ossMetadata);
//                                }finally {
//                                    ossClient.shutdown();
//                                    objectContent.close();
//                                }
//                            }
//                        }
//
//                    } catch (Exception e) {
//                        logger.error("导出数据异常 ,存储桶名称 --> {}, 对象路径 --> {} ",
//                                content.getBucket(), content.getObject(), e);
//                    } finally {
//                        bosClient.shutdown();
//                    }
//
//
//                } else if (bosEventDto.getEventType().equalsIgnoreCase(BusinessConstant.DELETE_OBJECT)) {
//                    //删除阿里 OSS
//                    try {
//                        String targetBucketName = null;
//                        if(content.getBucket().equals("englishstudy")){
//                            targetBucketName = BusinessConstant.XIAOMI_BUCKET_ENGLISH_STUDY_PREFIX + content.getBucket() + BusinessConstant.XIAOMI_BUCKET_SUFFIX_PREFIX+ BusinessConstant.XIAOMI_BUCKET_SUFFIX;
//                        }else {
//                            targetBucketName = BusinessConstant.XIAOMI_BUCKET_PREFIX + content.getBucket() + BusinessConstant.XIAOMI_BUCKET_SUFFIX;
//                        }
//
//                        OSStorage.getInstance().deleteObject(targetBucketName, content.getObject());
//                    } catch (Exception e) {
//                        logger.error("删除数据异常 , 存储桶名称 --> {}, 对象路径 --> {} ",
//                                content.getBucket(), content.getObject(), e);
//
//                    }
//                }
//            }
//
//
//        }

    }

}
